class RecordController < ApplicationController
  before_filter :sys_before_log

  def getRecords
    total = Record.countRecords params[:name].to_s,params[:begin_time].to_s,params[:end_time].to_s,params[:track_id].to_s
    records = Record.findRecords params[:name].to_s,params[:begin_time].to_s,params[:end_time].to_s,params[:track_id].to_s,params[:start].to_i,params[:limit].to_i

    jsonStr="{root:"+records.to_json(:include => [:worker,:track])+",total:"+total.to_s+"}"
    render :text => jsonStr,:layout => false
  end

  def delete
    begin
      ids= params[:id][1..params[:id].length-2].split(',')
      Record.delete(ids)
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '删除异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  require "spreadsheet"

  def printRecord
    Spreadsheet.client_encoding = "UTF-8"
    bookString = StringIO.new
    book = Spreadsheet.open "#{Rails.root.to_s}/public/report_template/record_template.xls"
    records = Record.findRecords(params[:worker_name].to_s, params[:begin_time].to_s,
      params[:end_time].to_s, params[:track_id].to_s,0,10000)
    i = 1

    sheet1 = book.worksheet 0
    records.each do |record|
      sheet1[i,0] = record.worker.name
      sheet1[i,1] = record.team_name
      sheet1[i,2] = record.train_name
      sheet1[i,3] = record.track ? record.track.track_name : "未指定"
      sheet1[i,4] = record.check_time.strftime("%Y-%m-%d %H:%M")
      sheet1[i,5] = record.check_type == '0' ? "开始工作" : "结束工作"
      i += 1
    end
    book.write bookString
    send_data bookString.string,:filename => 'record.xls'
  end
end
